package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseFloor;
import com.maoye.ossp.sys.base.entity.SysBaseShop;
import com.maoye.ossp.sys.base.service.SysBaseFloorService;
import com.maoye.ossp.sys.base.service.SysBaseShopService;
@Controller
@RequestMapping(value = "/sys/base/floor/")
public class SysBaseFloorController extends BaseController {
	
	@Resource
	private SysBaseFloorService sysBaseFloorService ;
	
	@Resource
	private SysBaseShopService sysBaseShopService ;
	

	/**
	 * 获得楼层列表
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getFloorList")
	public String getFloorList(HttpServletRequest request, HttpServletResponse response,Model model,SysBaseFloor floor) {
		QueryFilter queryFilter = getQuerFilter(request,floor);
		Page<SysBaseFloor> page = (Page<SysBaseFloor>)sysBaseFloorService
				.find(queryFilter);
		Map<String, Object> map = new HashMap<String, Object>();
		List<SysBaseFloor>list=page.getList();
		for(int i=0;i<list.size();i++){	
			map.put("code",list.get(i).getParent());
			SysBaseShop shop =sysBaseShopService.getShopById(map);
			if(shop!=null){
			   list.get(i).setParentArea(shop.getName());
			}else{
				list.get(i).setParentArea("无");
			}
		} 
		SysBaseFloor sysBaseFloor=new SysBaseFloor();
		model.addAttribute("sysBaseFloor",sysBaseFloor);
		model.addAttribute("page", page);
		return resultPage("/sys/base/floor/getFloorList", model, request);
	}
	
	/**
	 * 根据门店Id加载楼层信息
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="getFloorByPId")
	public void  getFloorByPId (HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String pId=request.getParameter("pId");
		StringBuilder json = new StringBuilder();
		Map map = new HashMap();
		map.put("pid", pId);
		List<SysBaseFloor>floors = sysBaseFloorService.getFloorByPId(map);
		json.append("[");
		for(int i = 0;i<floors.size();i++){
			SysBaseFloor floor = floors.get(i);
			json.append("{").append("id:").append(floor.getCode()).append(",");
			json.append("pId:").append(floor.getParent()).append(",");
			json.append("name:\"").append(floor.getName()).append("\"").append(",");
			json.append("}");
			if(i < floors.size()-1){
				json.append(",");
			}		
		}
		json.append("]");
		out.print(json.toString());
		out.flush();
		out.close();
	} 
	
	@RequestMapping(value ="addFloor")
	public String addFloor(HttpServletRequest request,HttpServletResponse response,Model model){	
		SysBaseFloor sysBaseFloor=new SysBaseFloor();
		model.addAttribute("sysBaseFloor",sysBaseFloor);
		return "/sys/base/floor/add"; 
	}
	
	@RequestMapping(value ="checkFloorCode")
	public void checkFloorCode(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		String shopCode=request.getParameter("shopCode");
		map.put("code",code);
		map.put("shopCode",shopCode);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseFloorService.checkFloorCode(map)){
			json.put("resCode",0); //门店号可用
		}else{
			json.put("resCode",1);//门店号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	@RequestMapping(value ="saveFloor")
	public String saveFloor(SysBaseFloor floor,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		floor.setId(idGenerator.getUId());
		//设置创建人
		floor.setCreateBy("");
		sysBaseFloorService.saveFloor(floor);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getFloorList";
	}
	
	/**
	 * 查看门店信息
	 * @param region
	 * @param model
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping(value ="viewFloor")
	public String viewFloor(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String shopCode =request.getParameter("shopCode");
		String shopName =request.getParameter("shopName");
		Map map = new HashMap();
		map.put("code",code);
		map.put("shopCode",shopCode);
		SysBaseFloor sysBaseFloor=null;
		sysBaseFloor=sysBaseFloorService.getFloorById(map);
		if(sysBaseFloor!=null){
			//添加门店名称
			sysBaseFloor.setParentArea(shopName);
			model.addAttribute("floor",sysBaseFloor);
		}
		return "/sys/base/floor/view";
	}
	
	@RequestMapping(value ="updateFloor")
	public String updateFloor(HttpServletRequest request,HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String shopCode =request.getParameter("shopCode");
		String shopName =request.getParameter("shopName");
		Map map = new HashMap();
		map.put("code",code);
		map.put("shopCode",shopCode);
		SysBaseFloor sysBaseFloor=null;
		sysBaseFloor=sysBaseFloorService.getFloorById(map);
		if(sysBaseFloor!=null){
			//添加门店名称
			sysBaseFloor.setParentArea(shopName);
			model.addAttribute("cityName",sysBaseFloor.getCityName());
			model.addAttribute("shopName",sysBaseFloor.getParentArea());
			model.addAttribute("sysBaseFloor",sysBaseFloor);
		}
		return "/sys/base/floor/update";
	}
	
	@RequestMapping(value ="saveUpdateFloor")
	public String saveUpdateFloor(HttpServletRequest request,HttpServletResponse response,SysBaseFloor floor,RedirectAttributes redirectAttributes){
		floor.setId(idGenerator.getUId());
		//设置修改人
		floor.setUpdateBy("");
		sysBaseFloorService.saveUpdateFloor(floor);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getFloorList";
	}
	@RequestMapping(value ="deleteFloor")
	public String deleteFloor(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		sysBaseFloorService.deleteFloor(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getFloorList";
	}
}
